package org.apache.servicecomb.fence.edge;

import java.util.Map;

import org.apache.servicecomb.fence.token.OpenIDToken;
import org.apache.servicecomb.fence.vo.TokenResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.server.ResponseStatusException;

import reactor.core.publisher.Mono;

@RestController
public class AuthController {

  @Autowired
  private WebClient webClient;

  @PostMapping(path = "/v1/token")
  public Mono<TokenResponse> load(@RequestBody Map<String, String> parameters) {

    return webClient.post()
        .uri("/authentication/v1/token")
        .bodyValue(parameters)
        .retrieve()
        .bodyToMono(OpenIDToken.class)
        .map(TokenResponse::fromOpenIDToken);
  }
}
